﻿using System;
using System.Linq;
using System.Collections.Generic;
using Kunlun.Models;

namespace Kunlun.BusinessLogic
{
    public partial class KunlunCategory
    {
      

        public static Category GetModel(int CategoryID)
        {
            try
                {
                KunLunEntities entities = new KunLunEntities();
                var model = entities.Category.FirstOrDefault(m => m.ID == CategoryID);
                if (model != null)
                    return model;
                }
            catch (Exception ex)
            {
             WebLog.ErrorLog(ex, CategoryID.ToString());
             return new Category();
            }
            return new Category();
        }

        public static List<Category> GetListByParentID(int parentID)
        {
            KunLunEntities entities = new KunLunEntities();
            var categoryList = entities.Category.Where(i => i.IsVisible && i.ParentID == parentID);
            return categoryList.ToList();
        }

        public static List<Category> GetListByParentID(int parentID, bool isVis,string function)
        {
            KunLunEntities entities = new KunLunEntities();
             List<Category> list = new List<Category>();
             try
                 {
                 
                 list = entities.Category.Where(i => i.IsVisible == isVis && i.ParentID == parentID).ToList();
                 }
             catch(Exception ex)
             {
             WebLog.ErrorLog(ex, "父类：" + function + parentID.ToString());
                 }
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cid">类别ID</param>
        /// <param name="ClassSortLevel">类别最大等级</param>
        /// <returns></returns>
        public static List<Category> GetCategoryMenuList()
            {
            KunLunEntities entities = new KunLunEntities();
            var categoryList = entities.Category.Where(i => i.IsVisible);
            return categoryList.ToList();
        }

        /// <summary>
        /// 根据框画的二级获取所有子类
        /// </summary>
        /// <returns></returns>
        public static List<Category> GetCategortByTwo(int cid)
            {
            KunLunEntities entities = new KunLunEntities();
            var result = new List<Category>();
            var model = entities.Category.FirstOrDefault(m => m.ID == cid);
            if (model != null)
            {
                if (model.Sort > 1)
                {
                    result = GetListByParentID(cid);
                }
            }
            return result;
        }

        /// <summary>
        /// MoreList左边导航列表
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static string GetLeftMenuList(int cid, int MaxSortLevel)
            {
            KunLunEntities entities = new KunLunEntities();
            // List<Category> menulist = new List<Category>();
            var curmodel = entities.Category.FirstOrDefault(m => m.ID == cid);

            string classids = "";   //类别字符集合 

            if (curmodel != null)
            {
                classids = curmodel.ID.ToString() + ",";
                int sort = curmodel.Sort;   // 输出1
                int index = 0;

                string[] templist = new string[MaxSortLevel - sort + 1];
                templist[index] = curmodel.ID.ToString();

                while (index < MaxSortLevel - sort)
                {
                    if (templist[index] == null || templist[index].Length <= 0)
                    {
                        break;
                    }
                    foreach (string tempid in Kaca.Core.Common.Utils.SplitString(templist[index], ","))
                    {
                        if (string.IsNullOrEmpty(tempid)) continue;
                        int seleID = Kaca.Core.Common.Utils.StrToInt(tempid, 0);
                        var q = entities.Category.Where(m => m.ParentID == seleID).Where(m => m.IsVisible == true);
                        if (q.ToList().Count > 0)
                        {
                            foreach (Category dr in q)
                            {
                                templist[index + 1] += dr.ID.ToString() + ",";
                                classids += dr.ID.ToString() + ",";
                            }
                        }
                    }
                    index++;
                }
            }

            return classids;
        }

        public static List<Category> GetMoreListMenu(int cid, int MaxSortLevel)
        {
            KunLunEntities entities = new KunLunEntities();
            string strWhere = GetLeftMenuList(cid, MaxSortLevel);

            strWhere = strWhere.Substring(0, strWhere.Length - 1);

            string[] input = strWhere.Split(',');

            int[] catelist = Array.ConvertAll<string, int>(input, delegate(string s) { return int.Parse(s); });

            var q = from p in entities.Category
                    where catelist.Contains(p.ID)
                    select p;

            return q.ToList();
        }

        ///<summary>
        ///获取类别下所有子类
        ///</summary>
        ///<param name="cid"></param>
        ///<returns></returns>
        public static List<Category> GetCategoryListByParentID(int cid)
        {
        KunLunEntities entities = new KunLunEntities();
            var result = entities.Category.Where(m => m.ParentID == cid);
            if (result != null)
            {
                return result.ToList();
            }
            return new List<Category>();
        }
        public static List<Category> GetIndexMenu()
        {
        
        List<Category> list = new List<Category>();
        try {
        list = Kunlun.BusinessLogic.KunlunCategory.GetListByParentID(38, true, "GetIndexMenu").Take(6).ToList();
                  
            }
        catch(Exception ex) {
             WebLog.ErrorLog(ex);
            }
            return list;
         }
    }
}
